package servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.User;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger=Logger.getLogger(LoginServlet.class);
    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info((User)request.getSession().getAttribute("user"));
		response.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		String psd=request.getParameter("password");
		
		UserDao userDao = new UserDaoImpl();
		User user=userDao.findUserByName(name);
		
		System.out.println(user);
		if(user.getPassword().equals(psd))
		{
			request.getSession().setAttribute("user",user);
			request.getRequestDispatcher("bookPurchase.jsp").forward(request, response);
			return;
		}
		else {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	

}
